package com.banyuan.project_atm;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BankServer {

    Account[] accounts = {
            new Account("白展堂", "155791", 1000),
            new Account("佟湘玉", "952711", 5000000),
            new Account("吕轻侯", "123456", 100000),
            new Account("郭芙蓉", "131466", 50),
            new Account("李秀莲", "155791", 3000),

    };
    //校验卡号
    //初始化
    Account temp = null;
 /*   public void initialize(Account account){
         account = null;
    }*/

    public void initialize1() {
        temp1 = null;
    }

    public void initialize2() {
        temp2 = null;
    }

    Account temp1 = null;
    Account temp2 = null;

    public void showMoney() {
        System.out.println("当前账户:" + temp1.getId() + "余额为：" + temp1.getMoney());
    }

    public String checkCarno(String cardno) {
        for (Account account : accounts) {
            if (account.id.equals(cardno)) {
                temp1 = account;
            }
        }
        if (temp1 == null) {
            return "帐号不存在!";
        }
        return ("卡号输入正确" + "当前账户是：" + temp1.id);
    }

    public String checkPassword(String password) {
        if (!temp1.password.equals(password)) {
            return "密码不正确！";
        }

        return "进入操作界面";

    }

    //查询
    public String queryMoney() {
        return "当前账户余额为：" + temp1.getMoney();

    }

    //取钱

    public String checkMoney(int money) {
//        System.out.println(temp1.getMoney());
        if (temp1.getMoney() < money) {
//            System.out.println(temp1.getMoney());
            return "余额不足！";
        }
        return "余额充足！";
    }

    public String getMoney(int money) {
        String stringGM = checkMoney(money);
        if (stringGM.equals("余额不足!")) {
            return "余额不足！";
        } else {
            temp1.setMoney(temp1.getMoney() - money);
            return queryMoney();
        }
    }

    //存钱
    public String putMoney(int money) {
        addMoney(temp1, money);
        return queryMoney();
    }

    //转账

    public String transferMoney(String cardno, int money) {
        for (Account account : accounts) {
            if (account.id.equals(cardno)) {
                temp2 = account;
            }
        }
        if (temp2 == null) {
            return "帐号不存在！";
        }

        getMoney(money);
        addMoney(temp2, money);
        return "转账成功!";
    }

    //功能区
    //向指定账户增加money；
    public void addMoney(Account account, int money) {
        account.setMoney(account.getMoney() + money);
    }

    //通过id查询服务器中有无该账号
    public String checkAccount(String id) {
        for (Account account : accounts) {
            if (account.id.equals(id)) {
                temp1 = account;
            }
        }
        if (temp1 == null) {
            return "账号不存在！";
        }
        return ("卡号输入正确" + "当前账户是：" + temp1.id);
    }

    public String checkAccount1(String id) {
        for (Account account : accounts) {
            if (account.id.equals(id)) {
                temp2 = account;
            }
        }
        if (temp2 == null) {
            return "账号不存在!";
        }
        return ("卡号输入正确" + "当前账户是：" + temp2.id);
    }

    //检出转出账户和转入账户是否为同一账户
    public String checkOne(String id){
        if(temp1 == temp2){
            return "相同";
        }
        return "不同";
    }

    //打印凭条
    Date now = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String stringNow = format.format(now);
    public void printVoucher (String s,int money){
        System.out.println("当前账户：" + temp1.getId());
        System.out.println("于" + stringNow);
        System.out.println(s + money + "元；");

    }

    //获取转账人的账户名称
    public String getTransferId (){
        return temp2.getId();
    }



}

